/**
 * 
 */
package com.usc.entme;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.Attributes;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.util.Log;

/**
 * @author Dhruvil
 *
 */
public class RssHandler  extends DefaultHandler{
	private String TAG = "Message";

	boolean atom = false;
	boolean title = false;
	boolean description = false;
	boolean link = false;
	boolean pubdate = false;
	boolean thumbnail=false;
	
	List<Message> messages = new ArrayList<Message>();
	Message currentMessage = new Message();
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.characters(ch, start, length);
		
		if(atom)
		{
			System.out.println("Atom : "+ new String(ch,start,length));
			atom = false;
		}
		if(title)
		{
			
			//System.out.println("Title : "+ new String(ch,start,length));
			currentMessage.setTitle(new String(ch,start,length));
			title = false;
		}
		if(link)
		{
			//System.out.println("Link : "+ new String(ch,start,length));
			currentMessage.setLink(new String(ch,start,length));
			link = false;
		}
		if(description)
		{
			//System.out.println("Description : "+ new String(ch,start,length));
			currentMessage.setDescription(new String(ch,start,length));
			description = false;
		}
		if(pubdate)
		{
			System.out.println("Pubdate : "+ new String(ch,start,length));
			pubdate = false;
		}
		
		
		
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		super.endElement(uri, localName, qName);
		//Log.e(TAG,"qname end : "+qName);
		if(qName.equalsIgnoreCase("ITEM"))
		{
			messages.add(currentMessage.copy());
			Log.e(TAG,"message title = " +currentMessage.getTitle());
			Log.e(TAG,"Images = " +currentMessage.getImg());
			//item = false;
		}
		if(qName.equalsIgnoreCase("ATOM"))
			atom = false;
		if(qName.equalsIgnoreCase("TITLE"))
			title = false;
		if(qName.equalsIgnoreCase("LINK"))
			link = false;
		if(qName.equalsIgnoreCase("DESCRIPTION"))
			description = false;
		if(qName.equalsIgnoreCase("PUBDATE"))
			pubdate = false;
		
	}

	
	@Override
	public void startElement(String uri, String localName, String qName,
			org.xml.sax.Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		super.startElement(uri, localName, qName, attributes);
		if(qName.equalsIgnoreCase("ATOM"))
			atom = true;
		if(qName.equalsIgnoreCase("TITLE"))
			title = true;
		if(qName.equalsIgnoreCase("LINK"))
			link = true;
		if(qName.equalsIgnoreCase("DESCRIPTION"))
			description = true;
		if(qName.equalsIgnoreCase("PUBDATE"))
			pubdate=true;
		if(qName.equalsIgnoreCase("media:thumbnail"))
		{	
			thumbnail=true;
			if (attributes.getValue("width").equals("66") && attributes.getValue("height").equals("49"))
			{
				//save this link only when we have small image
				String url=attributes.getValue("url");
				Log.e(TAG, "The URL needed is "+url);
				currentMessage.setImg(url);
			}
			//Log.e(TAG,(attributes.getValue("width")).toString());
		
		}	
	}
	
	
	public List<Message> getMessages()
	{
		return messages;
		
	}
	
}
